 import CacheUtil from '@/main/common/CacheUtil.js';
 
 function isOpen(){
	return new Promise(async(resolve, reject) => {
		let showFlag = false;
		try{
			let vipManage = CacheUtil.getTicketOpen();
			console.log('vipManage: ', vipManage);
			if(CacheUtil.getLogin()){
				if (1 == vipManage || 2 == vipManage) {
					uni.$http.getUser().then(res=>{
						if (0 != res.data.ticket) {
							if (1 == vipManage  //门票开
								|| getApp().globalData.openTicketMin //门票3分钟:已到
								|| CacheUtil.getTicketOver()//门票3分钟已到：再次启动
							){
								showFlag = true;
								resolve(showFlag)
								// console.log('showFlag: ', showFlag);
							} else {
								runTicket()
							}
						}
					})
				}
			}
		}catch(e){
		}
	})
}

function runTicket(){
	if (0 == getApp().globalData.ticketStartTime) {
		let timeId = setInterval(() => {
			console.log('TicketUtils--ticket--start', getApp().globalData.ticketStartTime);
			getApp().globalData.ticketStartTime++
			if (getApp().globalData.ticketStartTime == getApp().globalData.ticketTotalTime) {
				clearInterval(timeId)
				CacheUtil.setTicketOver()
				getApp().globalData.ticketStartTime = 0
				getApp().globalData.openTicketMin = true
				uni.reLaunch({
					url: '/pages/tab/home/home',
				});
			}
		}, 1000)
	}
}

module.exports = {
	isOpen
}